package com.sc.ifs.goods;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.model.goods.GoodsClass;
import com.sc.service.goods.GoodsClassService;
import com.sc.util.data.DataUtil;
import com.sc.util.exception.ExceptionEnum;

/**
 * 商品分类管理
 * @author maoyongbin
 * @日期 Mar 13, 2017 10:22:35 AM
 * @修改日期 2017.6.15 09:00:00 AM
 *  @xiongyong
 */
@Controller
@RequestMapping(value="app/goodsClass")
public class GoodsClassController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private GoodsClassService goodsClassService;
	
	/**
	 * 根据id，查找商品分类
	 * @param id
	 * @return Map<String, Object>
	 */
	@RequestMapping(value="/selectGoodsClass", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> selectGoodsClass(Integer id) {
		Map<String, Object> resultMap = new LinkedHashMap<String,Object>();
		if(id==null){
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_PARAM);
			logger.warn("查找参数有误！");
		}else{
			try{
				GoodsClass goodsClass = this.goodsClassService.selectByPrimaryKey(id);
				if(goodsClass!=null){
					resultMap.put("data", goodsClass);
					DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
					logger.warn("查找商品分类成功！");
				}else{
					DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
					logger.warn("查找商品分类失败！");
				}
			}catch (Exception e){
				e.printStackTrace();
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
				logger.warn("查找商品分类出错！");
			}
		}
		
		return resultMap;
	}
	
	/**
	 * 查找所有商品分类
	 * @return Map<String, Object>
	 */
	@RequestMapping(value="/selectList", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> selectList(HttpServletRequest request) {
		Map<String, Object> resultMap = new LinkedHashMap<String,Object>();
		//设置查询参数
		Map<String,Object> paramMap = new HashMap<String,Object>();
		String depth = request.getParameter("depth");
		String pId = request.getParameter("parentId");
		Integer parentId = null;
		String code = request.getParameter("code");
		String status = request.getParameter("status");
		if(StringUtils.isNotBlank(depth)){	//父级层级
			paramMap.put("depth", depth);
		}
		if(StringUtils.isNotBlank(pId)){	//父级分类id
			parentId = Integer.valueOf(pId);
			paramMap.put("parentId", pId);
		}
		if(StringUtils.isNotBlank(code)){	//分类编码
			paramMap.put("code", code);
		}
		if(StringUtils.isNotBlank(status)){	//分类状态，设置app首页的一级分类
			paramMap.put("status", status);
		}
		
		try{
			List<GoodsClass> goodsClassList = this.goodsClassService.selectList(paramMap);
			if(goodsClassList!=null && goodsClassList.size()>0){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
				resultMap.put("data", DataUtil.getGoodsCateList(goodsClassList, parentId));
				logger.warn("查找所有商品分类成功！");
			}else{
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
				logger.warn("查找所有商品分类失败！");
			}
		}catch (Exception e){
			e.printStackTrace();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.warn("查找所有商品分类出错！");
		}
		
		return resultMap;
	} 
	
	
	
	/**
	 * 查找所有商品分类
	 * @return Map<String, Object>
	 */
	@RequestMapping(value="/selectTreeList", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> selectTreeList(HttpServletRequest request) {
		Map<String, Object> resultMap = new LinkedHashMap<String,Object>();
		//设置查询参数
		String pId = request.getParameter("parentId");
		Integer parentId = null;
		GoodsClass goodsList = null;
		List<GoodsClass> dataList = null;
		if(StringUtils.isNotBlank(pId)){	//父级分类id
			parentId = Integer.valueOf(pId);
//			goodsList = this.resultTree(parentId);
			dataList =  this.resultTreeExt(parentId);
		}
		try{
			if(dataList!=null){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			//	resultMap.put("data", goodsList);
				resultMap.put("data", dataList);
				logger.warn("查找所有商品分类成功！");
			}else{
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
				logger.warn("查找所有商品分类失败！");
			}
		}catch (Exception e){
			e.printStackTrace();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.warn("查找所有商品分类出错！");
		}
		return resultMap;
	} 
	
	
	
	/***
	 *  集合递归迭代树结构
	 * @param pId
	 * @return
	 */
	public List<GoodsClass> resultTreeExt(Integer pId){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<GoodsClass> goodsClassExt = new ArrayList<GoodsClass>();
		paramMap.put("parentId", pId);
		List<GoodsClass> goodsClassExtList = this.goodsClassService.selectList(paramMap);
		for (GoodsClass goodsClass : goodsClassExtList) {
			GoodsClass gc = resultTree(goodsClass.getId());
			goodsClassExt.add(gc);
		}
		return goodsClassExt;
	}
	
	/***
	 *  单个对象递归迭代树结构
	 * @param pId
	 * @return
	 */
	public GoodsClass resultTree(Integer pId){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("classId", pId);
		List<GoodsClass> goodsClassList = this.goodsClassService.selectList(paramMap);
		GoodsClass classList = goodsClassList.get(0);
		Map<String, Object> paramExtMap = new HashMap<String, Object>();
		paramExtMap.put("parentId", pId);
		List<GoodsClass> goodsClassExtList = this.goodsClassService.selectList(paramExtMap);
		for (GoodsClass goodsClass : goodsClassExtList) {
			GoodsClass gc = resultTree(goodsClass.getId());
			classList.getChildren().add(gc);
		}
		return classList;
	}
	
	
	
}
